
Partial Class WebForm_ManageBulletin_SearchBulletin
    Inherits My.MyUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            RaiseEvent Loaded(Me, New EventArgs)
        End If
    End Sub
    
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        If Me.DelData(e.RowIndex) = True Then
            Me.BindGV()
        End If

    End Sub

    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Dim key As DataKey = Me.gv.DataKeys(e.RowIndex)
        Me.SelectedBulletinID = key("BulletinID")
        RaiseEvent Updating(Me, New EventArgs)
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    
End Class

Partial Class WebForm_ManageBulletin_SearchBulletin


    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select b.*,CONVERT(char(10), issuedate, 120) issuedate_date,c.text strusertype,d.text strtopmost ")
        sql.AppendFormat("from Bulletin b INNER JOIN codemapping c on b.usertype=c.value ")
        sql.AppendFormat("INNER JOIN codemapping d on b.topmost=d.value ")
        sql.AppendFormat("where c.tablename='bulletin' and c.columnname='UserType' ")
        sql.AppendFormat("and d.tablename='bulletin' and d.columnname='TopMost' ")
        sql.AppendFormat(Me.GetWhereSQL())

        dt = dat.GetData(sql)
        Me.gv.DataSource = dt
        Me.gv.DataBind()

    End Sub

    Public Sub BindGV(ByVal bulletinSubject As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select b.*,CONVERT(char(10), issuedate, 120) issuedate_date,c.text strusertype,d.text strtopmost ")
        sql.AppendFormat("from Bulletin b INNER JOIN codemapping c on b.usertype=c.value ")
        sql.AppendFormat("INNER JOIN codemapping d on b.topmost=d.value ")
        sql.AppendFormat("where c.tablename='bulletin' and c.columnname='UserType' ")
        sql.AppendFormat("and d.tablename='bulletin' and d.columnname='TopMost' ")
        sql.AppendFormat("and b.BulletinSubject = '{0}' ", bulletinSubject)
        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder
        
        If Me.txtBulletinSubject.Text <> "" Then
            sql.AppendFormat("and b.BulletinSubject like '%{0}%'", Me.txtBulletinSubject.Text.Replace("'", "''"))
        End If

        If Me.ddlUserType.Text <> "A" Then
            sql.AppendFormat("and (b.usertype = '{0}' or b.usertype = 'A')", Me.ddlUserType.Text)
        Else
            sql.AppendFormat("and b.usertype = 'A'")
        End If

        If Me.txtStartDate.Text <> "" Then            
            sql.AppendFormat("and issuedate >= '{0}'", Me.txtStartDate.Text.Replace("'", "''"))
        End If

        If Me.txtEndDate.Text <> "" Then            
            sql.AppendFormat("and issuedate <= '{0}'", Me.txtEndDate.Text.Replace("'", "''"))
        End If


        Return sql.ToString()
    End Function

    Private Function DelData(ByVal rowIndex As Integer) As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim key As DataKey = Me.gv.DataKeys(rowIndex)

        Using scope As New Transactions.TransactionScope

            sql.Length = 0
            sql.AppendFormat("delete Bulletin ")
            sql.AppendFormat(" where BulletinID = '{0}' ", key("BulletinID"))
            dat.AccData(sql)

            scope.Complete()
        End Using

        Return True
    End Function

    Public Property SelectedBulletinID() As String
        Get
            Return ViewState("SelectedBulletinID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedBulletinID") = value
        End Set
    End Property
    Public Property SelectedBulletinSubject() As String
        Get
            Return ViewState("SelectedBulletinSubject")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedBulletinSubject") = value
        End Set
    End Property


    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)
    Public Event Adding(ByVal source As Object, ByVal e As EventArgs)
    Public Event Updating(ByVal source As Object, ByVal e As EventArgs)
End Class

